using System;
using System.IO;
using System.Net.Mail;
using System.Text;
using Karma.Framework.Core.IoC;
using log4net;

namespace Karma.Framework.Core.Services.Infrastructure
{
    public class EmailService : IEmailService
    {
        private static ILog LOG = LogManager.GetLogger(
            typeof(EmailService));

        private MailMessage CreateMessage(string from, string[] to, string subject, 
            string message, Encoding encoding, bool isBodyHtml)
        {
            var fromAddress = new MailAddress(from);
            var mailMessage = new MailMessage();
            mailMessage.IsBodyHtml = isBodyHtml;
            mailMessage.From = fromAddress;
            foreach (var s in to)
            {
                mailMessage.To.Add(new MailAddress(s));
            }
            mailMessage.Subject = subject;
            mailMessage.SubjectEncoding = Encoding.UTF8;
            mailMessage.Body = message;
            mailMessage.BodyEncoding = encoding;

            return mailMessage;
        }

        private bool SendEmail(MailMessage message)
        {
            var client = new SmtpClient();
            try
            {
                client.Send(message);
                return true;
            }
            catch (Exception e)
            {
                if (LOG.IsErrorEnabled)
                {
                    LOG.Error(string.Format("An error ocurred while sending email message {0}.", e.Message),e);
                }
                return false;
            }
        }

        public bool SendEmail(string from, string[] to, string subject, string message,
            Encoding encoding, bool isBodyHtml)
        {
            var mailMessage = CreateMessage(from, to, subject, message, encoding, isBodyHtml);

            return SendEmail(mailMessage);
        }

        public bool SendEmail(string from, string[] to, string subject, string message,
            Encoding encoding, bool isBodyHtml, string[] attatchments, string[] mediaTypes)
        {
            var mailMessage = CreateMessage(from, to, subject, message, encoding, isBodyHtml);
            for (var idx = 0; idx < attatchments.Length; idx++)
            {
                mailMessage.Attachments.Add(
                    new Attachment(attatchments[idx], mediaTypes[idx]));
            }
            return SendEmail(mailMessage);
        }

        public bool SendEmail(string from, string[] to, string subject, string message,
            Encoding encoding, bool isBodyHtml, Stream[] attatchments, string[] mediaTypes)
        {
            var mailMessage = CreateMessage(from, to, subject, message, encoding, isBodyHtml);
            for (var idx = 0; idx < attatchments.Length; idx++)
            {
                mailMessage.Attachments.Add(
                    new Attachment(attatchments[idx], mediaTypes[idx]));
            }
            return SendEmail(mailMessage);
        }
    }
}